import { login, user } from '@/api/user'
import { Message } from 'element-ui'

export default {
  namespaced: true,
  state: {
    token: null,
    userId: null,
    userInfo: {}
  },
  mutations: {
    setToken (state, payload) {
      state.token = payload
    },
    setuserId (state, payload) {
      state.userId = payload
    },
    setUserInfo (state, payload) {
      state.userInfo = payload
    }
  },
  actions: {
    async loginAction (context, payload) {
      const { token, userId, success, msg } = await login(payload)
      // console.log(data)
      // console.log(userId)
      if (success) {
        context.commit('setToken', token)
        context.commit('setuserId', userId)
      } else {
        Message.error(msg)
        return Promise.reject(msg)
      }
    },
    async getUserInfo (context, id) {
      // console.log(id)
      const res = await user(id)
      // console.log(res.userName, res.userName.image)
      context.commit('setUserInfo', res)
    }
  }
}
